package actions;
import java.text.*;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.validator.DynaValidatorForm;
import beans.AbstractPagedQuery;
import service.VehicleService;
import beans.VehicleQuery;
import domain.Vehicle;
/**
 * @author system auto
 */
public class VehicleMainAction extends AbstractPagedQueryAction {
	/**
	 * 用于查询车辆.
	 */
	 private VehicleService vehicleService;
	/**
	  * 构造分页查询对象并进行分页查询.
	  * @param form action form
	  * @param request http request
	  * @param totalCount 记录总个数
	  * @param pagerOffset 查询的起始纪录位置
	  * @param itemsPerPage 每页多少条纪录
	  * @return AbstractPagedQuery Query
	  * @throws Exception any exception
	  */
	 @Override
	 public AbstractPagedQuery<Vehicle> doPagedQuery(ActionForm form,
	       HttpServletRequest request, int totalCount, int pagerOffset,
	       int itemsPerPage) throws Exception {
	   DynaValidatorForm queryCondForm = (DynaValidatorForm) form;
	   VehicleQuery myQuery = new VehicleQuery();
	   // 设置查询的分页参数
	   myQuery.setItemsPerPage(itemsPerPage);
	   myQuery.setTotalCount(totalCount);
	   myQuery.setPagerOffset(pagerOffset);
	   	String numCode = queryCondForm.getString("qNumCode");
		if (null != numCode && !"".equals(numCode)) {
	   		myQuery.setNumCode(numCode);
	   	}
	   	String carType = queryCondForm.getString("qCarType");
		if (null != carType && !"".equals(carType)) {
	   		myQuery.setCarType(carType);
	   	}
	   	String registNumber = queryCondForm.getString("qRegistNumber");
		if (null != registNumber && !"".equals(registNumber)) {
	   		myQuery.setRegistNumber(registNumber);
	   	}
	   	String chargeShipGRT = queryCondForm.getString("qChargeShipGRT");
		if (null != chargeShipGRT && !"".equals(chargeShipGRT)) {
	   		myQuery.setChargeShipGRT(Double.valueOf(chargeShipGRT));
	   	}
	   	String carName = queryCondForm.getString("qCarName");
		if (null != carName && !"".equals(carName)) {
	   		myQuery.setCarName(carName);
	   	}
	   	String carStatus = queryCondForm.getString("qCarStatus");
		if (null != carStatus && !"".equals(carStatus)) {
	   		myQuery.setCarStatus(carStatus);
	   	}
	   	String carVolume = queryCondForm.getString("qCarVolume");
		if (null != carVolume && !"".equals(carVolume)) {
	   		myQuery.setCarVolume(Double.valueOf(carVolume));
	   	}
	   	String carColor = queryCondForm.getString("qCarColor");
		if (null != carColor && !"".equals(carColor)) {
	   		myQuery.setCarColor(carColor);
	   	}
   		// 调用VehicleService进行分页查询
        vehicleService.queryVehicles(myQuery);
        return myQuery;
  	}
  /**
   * 批量删除车辆.
   * @param mapping mapping
   * @param form v
   * @param request
   * @param response
   * @return action forward
   * @throws Exception
   */
  public ActionForward batchDelete(ActionMapping mapping, ActionForm form,
          HttpServletRequest request, HttpServletResponse response)
          throws Exception {
      String[] ids = request.getParameterValues("ids");
      if (ids != null && !(ids.length == 0)) {
          vehicleService.delVehicleByIDs(ids);
      }
      return mapping.findForward("main");
  }
  /**
    * 显示新增界面.
    * @param mapping struts mapping
    * @param form struts form
    * @param request http request
    * @param response http response
    * @return action forward
    * @throws Exception any execption
    */
   public ActionForward displayVehicleCreateScreen(ActionMapping mapping,
           ActionForm form, HttpServletRequest request,
           HttpServletResponse response) throws Exception {
       // 产生并保存Token,防止重复提交
       saveToken(request);
       // 初始化输入框
       DynaValidatorForm vehicleForm = (DynaValidatorForm) mapping
               .getModuleConfig().findFormBeanConfig("vehicleForm")
               .createActionForm(servlet); 
        vehicleForm.set("id", ""); 
        vehicleForm.set("numCode", ""); 
        vehicleForm.set("manufactType", ""); 
        vehicleForm.set("carType", ""); 
        vehicleForm.set("registNumber", ""); 
        vehicleForm.set("carAge", ""); 
        vehicleForm.set("shipGRT", ""); 
        vehicleForm.set("engineNumber", ""); 
        vehicleForm.set("operaCert", ""); 
        vehicleForm.set("chargeShipGRT", ""); 
        vehicleForm.set("buyDate", ""); 
        vehicleForm.set("carName", ""); 
        vehicleForm.set("carNote", ""); 
        vehicleForm.set("carStage", ""); 
        vehicleForm.set("curPosition", ""); 
        vehicleForm.set("carStatus", ""); 
        vehicleForm.set("carVolume", ""); 
        vehicleForm.set("oilRate", ""); 
        vehicleForm.set("carBodyNumber", ""); 
        vehicleForm.set("carColor", "");
       // 将Struts form 设置到request中
       request.setAttribute("vehicleForm", vehicleForm);
       return mapping.findForward("VehicleCreate.screen");
   }
   /**
    * 显示更新操作.
    * @param mapping struts mapping
    * @param form struts form
    * @param request http request
    * @param response http response
    * @return action forward
    * @throws Exception any execption
    */
   public ActionForward displayVehicleUpdateScreen(ActionMapping mapping,
           ActionForm form, HttpServletRequest request,
           HttpServletResponse response) throws Exception {
       String id = request.getParameter("id");
       Vehicle vehicle = vehicleService.getVehicleById(id);
       if (vehicle == null) {
	       ActionMessages errMsgs = new ActionMessages();
	       errMsgs.add("", new ActionMessage("errors.noSuchVehicle"));
	       this.saveErrors(request, errMsgs);
	       return mapping.findForward("error.back");
       } 
       DynaValidatorForm vehicleForm = (DynaValidatorForm) mapping
               .getModuleConfig().findFormBeanConfig("vehicleForm")
               .createActionForm(servlet);
	       	   if(vehicle.getId()!=null){
	 	 	   vehicleForm.set("id",String.valueOf(vehicle.getId()));
	 	 	   }else{
	 	 	   vehicleForm.set("id",null);
	 	 	   }
	           vehicleForm.set("numCode",vehicle.getNumCode());
	           vehicleForm.set("manufactType",vehicle.getManufactType());
	           vehicleForm.set("carType",vehicle.getCarType());
	           vehicleForm.set("registNumber",vehicle.getRegistNumber());
	       	   if(vehicle.getCarAge()!=null){
	 	 	   vehicleForm.set("carAge",String.valueOf(vehicle.getCarAge()));
	 	 	   }else{
	 	 	   vehicleForm.set("carAge",null);
	 	 	   }
	       	   if(vehicle.getShipGRT()!=null){
	 	 	   vehicleForm.set("shipGRT",String.valueOf(vehicle.getShipGRT()));
	 	 	   }else{
	 	 	   vehicleForm.set("shipGRT",null);
	 	 	   }
	           vehicleForm.set("engineNumber",vehicle.getEngineNumber());
	           vehicleForm.set("operaCert",vehicle.getOperaCert());
	       	   if(vehicle.getChargeShipGRT()!=null){
	 	 	   vehicleForm.set("chargeShipGRT",String.valueOf(vehicle.getChargeShipGRT()));
	 	 	   }else{
	 	 	   vehicleForm.set("chargeShipGRT",null);
	 	 	   }
		       DateFormat DATE_FAMAT_BUYDATE = new SimpleDateFormat("yyyy-MM-dd",Locale.UK);
		       if(vehicle.getBuyDate()!=null){
		       vehicleForm.set("buyDate", DATE_FAMAT_BUYDATE.format(vehicle.getBuyDate()));}
		       else{vehicleForm.set("buyDate",null);}
	           vehicleForm.set("carName",vehicle.getCarName());
	           vehicleForm.set("carNote",vehicle.getCarNote());
	           vehicleForm.set("carStage",vehicle.getCarStage());
	           vehicleForm.set("curPosition",vehicle.getCurPosition());
	           vehicleForm.set("carStatus",vehicle.getCarStatus());
	       	   if(vehicle.getCarVolume()!=null){
	 	 	   vehicleForm.set("carVolume",String.valueOf(vehicle.getCarVolume()));
	 	 	   }else{
	 	 	   vehicleForm.set("carVolume",null);
	 	 	   }
	       	   if(vehicle.getOilRate()!=null){
	 	 	   vehicleForm.set("oilRate",String.valueOf(vehicle.getOilRate()));
	 	 	   }else{
	 	 	   vehicleForm.set("oilRate",null);
	 	 	   }
	           vehicleForm.set("carBodyNumber",vehicle.getCarBodyNumber());
	           vehicleForm.set("carColor",vehicle.getCarColor());
       // 将Struts form 设置到request中
       request.setAttribute("vehicleForm", vehicleForm);
       // 设置token 防止重复提交
       this.saveToken(request);
       return mapping.findForward("VehicleUpdate.screen");
   }
	/**
    * 显示预览.
    * @param mapping struts mapping
    * @param form struts form
    * @param request http request
    * @param response http response
    * @return action forward
    * @throws Exception any execption
    */
   public ActionForward displayVehicleBrowseScreen(ActionMapping mapping,
           ActionForm form, HttpServletRequest request,
           HttpServletResponse response) throws Exception {
       String id = request.getParameter("id");
       Vehicle vehicle = vehicleService.getVehicleById(id);
       if (vehicle == null) {
	       ActionMessages errMsgs = new ActionMessages();
	       errMsgs.add("", new ActionMessage("errors.noSuchVehicle"));
	       this.saveErrors(request, errMsgs);
	       return mapping.findForward("error.back");
       } 
   	   request.setAttribute("RQ_VEHICLE",vehicle);
       return mapping.findForward("VehicleBrowse.screen");
   }
   /**
    * @param pVehicleService the vehicleService to set
    */
   public void setVehicleService(VehicleService pVehicleService) {
       vehicleService = pVehicleService;
   }
}
